polymorphism: better names for .binaryValue and .booleanValue are .asInteger and...
[supercollider.git] / Help / Other Topics / Tracing Processes.html
blob34bb15fadab1462caeae7a015e08d27a4d197191
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.46">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; min-height: 17.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
22 span.s1 {color: #000000}
23 span.s2 {text-decoration: underline}
24 span.s3 {color: #0000bf}
25 span.s4 {color: #007300}
26 span.s5 {color: #606060}
27 span.s6 {color: #0000bf}
28 span.s7 {color: #0000bf}
29 span.s8 {color: #0000bf}
30 span.s9 {color: #0000bf}
31 span.s10 {color: #0000bf}
32 span.Apple-tab-span {white-space:pre}
33 </style>
34 </head>
35 <body>
36 <p class="p1"><b>Tracing processes in SC</b></p>
37 <p class="p2"><br></p>
38 <p class="p3">What goes on in a running system? In SC, various methods help to get information about processes on different levels: server side and client side (in sclang).</p>
39 <p class="p2"><br></p>
40 <p class="p4"><span class="s1">related:<span class="Apple-converted-space">  </span><a href="../Language/Debugging-tips.html"><span class="s2">Debugging-tips</span></a></span></p>
41 <p class="p2"><br></p>
42 <p class="p5"><b>Tracing sclang processes:</b></p>
43 <p class="p6"><br></p>
44 <p class="p3">In order to know more about objects as they are created by processes like tasks or even simply by evaluating a bit of code, one can insert messages like postln and postcs anywhere in the code.</p>
45 <p class="p6"><br></p>
46 <p class="p7">// calculating the sum of n subsequent squares</p>
47 <p class="p8">(</p>
48 <p class="p8"><span class="s3">var</span> n = 8, x = 0;</p>
49 <p class="p8">(1..n).do { <span class="s3">|num|</span> x = x + num.squared };</p>
50 <p class="p8">x</p>
51 <p class="p8">)</p>
52 <p class="p9"><br></p>
53 <p class="p7">// what happens while we are doing this?</p>
54 <p class="p8">(</p>
55 <p class="p8"><span class="s3">var</span> n = 8, x = 0;</p>
56 <p class="p8">(1..n).do { <span class="s3">|num|</span> x = x + num.squared.postln; };</p>
57 <p class="p8">x</p>
58 <p class="p8">)</p>
59 <p class="p9"><br></p>
60 <p class="p7">// or more in detail:</p>
61 <p class="p8">(</p>
62 <p class="p8"><span class="s3">var</span> n = 8, x = 0;</p>
63 <p class="p8">(1..n).do { <span class="s3">|num|</span> [<span class="s4">\before</span>, x].postln; x = x + num.squared; [<span class="s4">\after</span>, x].postln;};</p>
64 <p class="p8">x</p>
65 <p class="p8">)</p>
66 <p class="p9"><br></p>
67 <p class="p9"><br></p>
68 <p class="p7">// when posting several values, some more verbose posts can be useful.</p>
69 <p class="p7">// postf formats a string and inserts values for %-characters.</p>
70 <p class="p7">// here separate statements are needed.</p>
71 <p class="p9"><br></p>
72 <p class="p8">(</p>
73 <p class="p8"><span class="s3">var</span> n = 8, x = 0;</p>
74 <p class="p8">(1..n).do { <span class="s3">|num|</span> x = x + num.squared; <span class="s5">"num: % num-squared: % new x: %\n"</span>.postf(num, num.squared, x) };</p>
75 <p class="p8">x</p>
76 <p class="p8">)<span class="Apple-converted-space"> </span></p>
77 <p class="p9"><br></p>
78 <p class="p7">// in some cases, postln will only post part of the data, or a simplified</p>
79 <p class="p7">// representation.</p>
80 <p class="p9"><br></p>
81 <p class="p7">// n times 200 random numbers</p>
82 <p class="p7">// will just add ... etc ... after 123</p>
83 <p class="p8">(</p>
84 <p class="p8"><span class="s3">var</span> n = 3;</p>
85 <p class="p8">(1..n).do { <span class="s3">|num|</span> { 1000.rand }.dup(200).postln };</p>
86 <p class="p8">)</p>
87 <p class="p9"><br></p>
88 <p class="p7">// posts the compile string, i.e. the code needed to recreate the receiver (here the array)</p>
89 <p class="p8">(</p>
90 <p class="p8"><span class="s3">var</span> n = 3;</p>
91 <p class="p8">(1..n).do { <span class="s3">|num|</span> { num.rand }.dup(200).postcs };</p>
92 <p class="p8">)</p>
93 <p class="p9"><br></p>
94 <p class="p9"><br></p>
95 <p class="p7">// in streams, tasks and routines, this works just as well:</p>
96 <p class="p8">(</p>
97 <p class="p8">fork {<span class="Apple-tab-span"> </span></p>
98 <p class="p8"><span class="Apple-tab-span"> </span><span class="s3">var</span> n = 14;</p>
99 <p class="p8"><span class="Apple-tab-span"> </span>(1..n).do { <span class="s3">|num|</span><span class="Apple-converted-space"> </span></p>
100 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>{ num.rand }.dup(200).postcs;</p>
101 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.wait;</p>
102 <p class="p8"><span class="Apple-tab-span"> </span>};</p>
103 <p class="p8">}</p>
104 <p class="p8">)</p>
105 <p class="p9"><br></p>
106 <p class="p8">(</p>
107 <p class="p8">fork {<span class="Apple-tab-span"> </span></p>
108 <p class="p8"><span class="Apple-tab-span"> </span><span class="s3">var</span> str = <span class="s3">Routine</span> { <span class="s3">|in|</span> 10.do { in = in.rand.yield } };</p>
109 <p class="p8"><span class="Apple-tab-span"> </span>12.0.do { <span class="s3">|i|</span></p>
110 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>str.next(i).postln;</p>
111 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>0.5.wait;</p>
112 <p class="p8"><span class="Apple-tab-span"> </span>};</p>
113 <p class="p8">}</p>
114 <p class="p8">)</p>
115 <p class="p9"><br></p>
116 <p class="p9"><br></p>
117 <p class="p7">// for creating a pattern that once it is used posts its values,</p>
118 <p class="p7">// the message trace can be used (in returns a Ptrace)</p>
119 <p class="p9"><br></p>
120 <p class="p8">a = <span class="s3">Pseq</span>([1, 4, 1, <span class="s3">Pwhite</span>(0, 6, 3), 100, 39], <span class="s3">inf</span>).trace(prefix: <span class="s5">"value: "</span>);</p>
121 <p class="p8">b = a.asStream;</p>
122 <p class="p8">b.next;</p>
123 <p class="p8">b.next;</p>
124 <p class="p8">b.next;</p>
125 <p class="p8">b.next;</p>
126 <p class="p9"><br></p>
127 <p class="p7">// in a running stream:</p>
128 <p class="p8">(</p>
129 <p class="p10">Pbind<span class="s1">(</span></p>
130 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\degree</span>, <span class="s3">Pseq</span>([1, 4, 1, <span class="s3">Pwhite</span>(0, 6, 3), 100, 39], <span class="s3">inf</span>).trace(prefix: <span class="s5">"value: "</span>),</p>
131 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\dur</span>, 0.2</p>
132 <p class="p8">).play</p>
133 <p class="p8">);</p>
134 <p class="p9"><br></p>
135 <p class="p7">// post only a slot of the events</p>
136 <p class="p8">(</p>
137 <p class="p10">Pbind<span class="s1">(</span></p>
138 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\degree</span>, <span class="s3">Pseq</span>([1, 4, 1, <span class="s3">Pwhite</span>(0, 6, 3), 100, 39], <span class="s3">inf</span>),</p>
139 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\dur</span>, 0.2</p>
140 <p class="p8">).trace(<span class="s4">\degree</span>).play</p>
141 <p class="p8">);</p>
142 <p class="p6"><br></p>
143 <p class="p7">// several slots at once:</p>
144 <p class="p8">(</p>
145 <p class="p10">Pbind<span class="s1">(</span></p>
146 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\degree</span>, <span class="s3">Pseq</span>([1, 4, 1, <span class="s3">Pwhite</span>(0, 6, 3), 100, 39], <span class="s3">inf</span>),</p>
147 <p class="p8"><span class="Apple-tab-span"> </span><span class="s4">\dur</span>, <span class="s3">Pwhite</span>(0.2, 0.4, <span class="s3">inf</span>)</p>
148 <p class="p8">).trace([<span class="s4">\degree</span>, <span class="s4">\dur</span>], prefix: [<span class="s5">"degree "</span>, <span class="s5">"dur "</span>]).play</p>
149 <p class="p8">);</p>
150 <p class="p6"><br></p>
151 <p class="p6"><br></p>
152 <p class="p2"><br></p>
153 <p class="p5"><b>Tracing server processes:</b></p>
154 <p class="p6"><br></p>
155 <p class="p3">Using postln or post on a UGen will only return the UGen, but not the values it produces in a running synth. The poll message creates a Poll UGen which posts at regular intervals when given a time value or as a response to a trigger (see <a href="../UGens/Triggers/Poll.html"><span class="s6">Poll</span></a> helpfile)</p>
156 <p class="p6"><br></p>
157 <p class="p7">// postln returns only the UGen itself (a MulAdd here)</p>
158 <p class="p8"><span class="s3">SinOsc</span>.ar(<span class="s3">SinOsc</span>.kr(0.2, 0, 300, 400).postln) * 0.1 }.play;</p>
159 <p class="p9"><br></p>
160 <p class="p7">// poll traces the values</p>
161 <p class="p8"><span class="s3">SinOsc</span>.ar(<span class="s3">SinOsc</span>.kr(0.2, 0, 300, 400).poll) * 0.1 }.play;</p>
162 <p class="p9"><br></p>
163 <p class="p7">// using a label:</p>
164 <p class="p8"><span class="s3">SinOsc</span>.ar(<span class="s3">SinOsc</span>.kr(0.2, 0, 300, 400).poll(label: <span class="s5">"freq"</span>)) * 0.1 }.play;</p>
165 <p class="p6"><br></p>
166 <p class="p6"><br></p>
167 <p class="p3">For <b>demand ugens</b>, poll does not work - these ugens are called by a Demand or Duty Ugen at certain intervals. The message dpoll creates a Dpoll ugen that posts when they are called (see <a href="../UGens/Synth control/Demand Rate/Dpoll.html"><span class="s6">Dpoll</span></a> helpfile)</p>
168 <p class="p6"><br></p>
169 <p class="p9"><br></p>
170 <p class="p8">{ <span class="s3">SinOsc</span>.ar(<span class="s3">Duty</span>.kr(0.5, 0, (<span class="s3">Dseries</span>(0, 1, <span class="s3">inf</span>) * 200 + 300).dpoll)) * 0.1 }.play;</p>
171 <p class="p9"><br></p>
172 <p class="p8">{ <span class="s3">SinOsc</span>.ar(<span class="s3">Duty</span>.kr(0.5, 0, (<span class="s3">Dseries</span>(0, 1, <span class="s3">inf</span>) * 200 + 300).dpoll(label: <span class="s5">"freq"</span>))) * 0.1 }.play;</p>
173 <p class="p9"><br></p>
174 <p class="p6"><br></p>
175 <p class="p3">When using the internal server (see <a href="../ServerArchitecture/Server.html"><span class="s7">Server</span></a> help), the <b>scope</b> window can give valuable information about the ongoing sound (see <a href="../GUI/Main-GUI/Stethoscope.html"><span class="s7">Stethoscope</span></a> help):</p>
176 <p class="p6"><br></p>
177 <p class="p11">// must use internal server</p>
178 <p class="p8">s = <span class="s8">Server</span>.internal.boot;</p>
179 <p class="p8"><span class="s9">SinOsc</span>.ar(<span class="s9">SinOsc</span>.kr(0.2, 0, 300, 400)) * 0.1 }.scope;</p>
180 <p class="p9"><br></p>
181 <p class="p3">A <a href="../GUI/Main-GUI/FreqScope.html"><span class="s10">FreqScope</span></a> window can be used for observing the spectrum of the output:</p>
182 <p class="p9"><br></p>
183 <p class="p11">// must use internal server</p>
184 <p class="p8"><span class="s3">Server</span>.default = s = <span class="s3">Server</span>.internal.boot;</p>
185 <p class="p9"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
186 <p class="p11">// create a new analyzer</p>
187 <p class="p12">FreqScope<span class="s1">.new;</span></p>
188 <p class="p9"><br></p>
189 <p class="p8"><span class="s9">SinOsc</span>.ar(<span class="s9">SinOsc</span>.ar(0.2, 0, 3000, 4000)) * 0.1 }.play;</p>
190 <p class="p8"><span class="s9">SinOsc</span>.ar(<span class="s9">SinOsc</span>.ar((1..4) * 0.02, 0, 3000, 4000)).sum * 0.1 }.play;</p>
191 <p class="p9"><br></p>
192 <p class="p9"><br></p>
193 <p class="p9"><br></p>
194 <p class="p9"><br></p>
195 </body>
196 </html>